<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新增工单</title>
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
  <legend>新增工单</legend>
</fieldset>

<form class="layui-form" action="#" lay-filter="add-orderForm" id="addOrderForm">
    <input name="id" type="hidden" value="0">
    <input name="parentId" type="hidden" value="0">
<div class="layui-form-item" >
    <label class="layui-form-label"><span class="redColor">*</span>工单类型:</label>
    <div class="layui-input-block">
      <input type="checkbox" name="orderFrom" lay-skin="primary" title="项目交付">
      <input type="checkbox" name="orderFrom" lay-skin="primary" title="对外测试">
      <input type="checkbox" name="orderFrom" lay-skin="primary" title="备件维修">
    </div>
     <div class="layui-input-block">
      <input type="checkbox" name="orderFrom" lay-skin="primary" title="网上问题">
      <input type="checkbox" name="orderFrom" lay-skin="primary" title="培训">
      <input type="checkbox" name="orderFrom" lay-skin="primary" title="其它">
    </div>
</div>
  <div class="layui-form-item">
  <div class="layui-input-inline"> 
    <label class="layui-form-label"><span class="redColor">*</span>客户:</label>
    <div class="layui-input-block" style="width:400px;">
      <input type="text" name="customerName" lay-verify="custom" placeholder="请输入客户名称" class="layui-input">
      <input type="hidden" name="orderNo">
      <input type="hidden" name="orderFromNo">
			<input type="hidden" name="onlineOrderFromNo" />
      <input type="hidden" name="orderFromName">
    </div>
    </div>
  </div>
  <div class="layui-form-item projectDelivery" >
    <label class="layui-form-label" id="type_title"><span class="redColor">*</span>项目名称:</label>
    <div class="layui-input-inline" style="width:400px;">
      <input type="text" name="projectName" lay-verify="projectName" placeholder="请选择项目名称" readonly class="layui-input projectName">
    </div>
    <div class="layui-form-mid layui-word-aux">
    	<i class="layui-icon layui-icon-add-circle" style="font-size:28px" id="projectDelivery"></i>
    </div>
  </div>
  
  <div class="layui-form-item onlineProblems">
    <label class="layui-form-label"><span class="redColor">*</span>网上问题:</label>
    <div class="layui-input-inline" style="width:400px;">
      <input type="text" name="onlineProblemsName" lay-verify="onlineProblemsName" placeholder="请选择网上问题" readonly class="layui-input onlineProblemsName">
    </div>
    <div class="layui-form-mid layui-word-aux">
    	<i class="layui-icon layui-icon-add-circle" style="font-size:28px" id="onlineProblems"></i>
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label" >客户地址:</label>
    <div class="layui-input-inline" style="width:500px;">
      <input type="text" name="customerAddress" placeholder="请输入客户地址" class="layui-input">
			<input type="hidden" name="customerLocation" id="customerLocation" />
    </div>
		<div class="layui-form-mid layui-word-aux">
				<i class="layui-icon layui-icon-location" style="font-size:28px" id="selectAddress"></i>
		</div>
  </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="redColor">*</span>联系人:</label>
        <div class="layui-input-block" style="width:400px;">
            <input type="text" name="contactName" lay-verify="contactName" placeholder="请输入联系人" class="layui-input">
        </div>
    </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label"><span class="redColor">*</span>联系电话:</label>
    <div class="layui-input-block" style="width:400px;">
      <input type="text" name="customerPhone" lay-verify="customerPhone" placeholder="请输入联系电话" class="layui-input">
    </div>
  </div>
  
   <div class="layui-form-item">
    <label class="layui-form-label"><span class="redColor">*</span>是否紧急:</label>
    <div class="layui-input-block" style="width:400px;">
      <input type="radio" name="isUrgent" value="0" title="是" >
      <input type="radio" name="isUrgent" value="1" title="否" checked="">
    </div>
  </div>
  
   <div class="layui-form-item layui-form-text">
    <label class="layui-form-label"><span class="redColor">*</span>任务描述</label>
    <div class="layui-input-block" style="width:400px;">
      <textarea placeholder="请输入任务描述" class="layui-textarea" name="taskDesc" lay-verify="taskDesc"></textarea>
    </div>
  	</div>
  	
  	<div class="layui-form-item">
  	<label class="layui-form-label"><span class="redColor">*</span>预约时间</label>
		 <div class="layui-input-block" style="width:400px;">
			<input type="text" class="layui-input" id="planTime" name="planTime" lay-verify="planTime">
		</div>
  	</div>
   <!--  <div class="layui-form-item handle">
     <label class="layui-form-label">选择工程师:</label>
    <div class="layui-input-inline" style="width:400px;">
      <input type="text" name="handle" lay-verify="handler" placeholder="请选择工程师" class="layui-input" readonly>
      <input type="hidden" name="handleNo"/>
      <input type="hidden" name="handlerDept"/>
    </div>
    <div class="layui-form-mid layui-word-aux">
    	<i class="layui-icon layui-icon-add-circle" style="font-size:28px" id="selectHandler"></i>
    </div>
    </div> -->
    <div class="layui-form-item region">
        <label class="layui-form-label">办事处/部门:</label>
        <div class="layui-input-inline" style="width:400px;">
            <input type="text" name="regionName" id="regionName" lay-verify="handler" readonly="readonly" class="layui-input">
            <input type="hidden" name="handle" id="handle">
            <input type="hidden" name="handleNo" id="handleNo"/>
            <input type="hidden" name="handlerDept" id="handlerDept"/>
        </div>
        <div class="layui-form-mid layui-word-aux">
            <i class="layui-icon layui-icon-add-circle" style="font-size:28px" id="selectRegion"></i>
        </div>
    </div>
		
		<div class="layui-form-item region">
				<label class="layui-form-label">抄送人:</label>
				<div class="layui-input-inline"  style="width:400px;" >
						<input style="display: inline-block;padding-right: 42px;" type="text" name="ccName" id="ccName" readonly="readonly" class="layui-input">
						<div class="cus-btn" id="remove-cc">清除</div>
				</div>
				<div class="layui-form-mid layui-word-aux">
						<i class="layui-icon layui-icon-add-circle" style="font-size:28px" id="selectCC"></i>
				</div>
		</div>
  	<div class="layui-form-item"  >
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit="" lay-filter="add-order">提交</button>
      <!-- <button class="layui-btn  layui-btn-normal" lay-submit="" lay-filter="add-draft">存为草稿</button> -->
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
 </form>

<script type="text/javascript">

	var form = layui.form
	,layer = layui.layer
	,laydate = layui.laydate;
	
	var clickType = 0;
	var selectCCList = [];
	//默认1是服务经理
	var isManage = 1;
	
	laydate.render({
		elem : '#planTime' //指定元素
		,type: 'datetime',
		format: 'yyyy-MM-dd HH:mm:ss'
	});
	
	var addForm = form.render(null,'add-orderForm');
	
	addForm.verify({
		custom : function(value, item) { //value：表单的值、item：表单的DOM对象
			if (value.trim() == '') {
				return '请输入客户名称';
			}
		},
		/*projectName: function(value,item) { //value：表单的值、item：表单的DOM对象
			if (value.trim() == '') {
				return '请输入工单类型名称';
			}
		},*/
		contactName:function(value,item) {
			var isExtreantTest = false;
			$('#addOrderForm').find('input[type=checkbox]').each(function(index, item2){
				var _title = $(item2)[0].title;
				var _checked = $(item2)[0].checked;
				if(_checked && _title == "对外测试"){
					isExtreantTest = true;
					return;
				}
			});
			if (!isExtreantTest && value.trim() == '') {
					return '请输入联系人';
			}
		},
		customerPhone:function(value,item) {
			var isExtreantTest = false;
			$('#addOrderForm').find('input[type=checkbox]').each(function(index, item2){
				var _title = $(item2)[0].title;
				var _checked = $(item2)[0].checked;
				if(_checked && _title == "对外测试"){
					isExtreantTest = true;
					return;
				}
			});
			if (!isExtreantTest && value.trim() == '') {
				return '请输入客户联系电话';
			} else {
				if(value.trim() != "" && !checkPhoneNum(value.trim())) {
					return '联系电话格式不正确';
				}
			}
		},
		/*customerAddress: function(value,item) { //value：表单的值、item：表单的DOM对象
			if (value.trim() == '') {
				return '请输入客户联系地址';
			}
		},*/
		taskDesc: function(value,item) { //value：表单的值、item：表单的DOM对象
			if (value.trim() == '') {
				return '请输入任务描述';
			}
		},
		planTime: function(value,item) { //value：表单的值、item：表单的DOM对象
			if (value.trim() == '') {
				$("#planTime").trigger("click");
				return '请选择预约时间';
			}
		},
		handler: function(value,item) { //value：表单的值、item：表单的DOM对象
				if (value.trim() == '') {
						return '请选择工程师或办事处';
				}
		}
	});
	
	function checkPhoneNum(phone) {
		var phonePatrn = /^((\+?86)|(\(\+86\)))?\d{3,4}-\d{7,8}(-\d{3,4})?$/;
	 	var mobilePatrn = /^((\+?86)|(\(\+86\)))?1\d{10}$/;
		if (!phonePatrn.exec(phone) && !mobilePatrn.exec(phone)) return false;
		return true;
	}
    //是否为经理
	function checkMannge() {
		var empCode = window.empCode;
		$.ajax({
				url : window.basePath + '/sys/checkIsManager',
				headers: {"token" : window.token},
				method: "post",
				data : {
						workNo : empCode
				},
				success : function (data) {
						/* if(data.message == '操作成功'){
								$(".region").remove();
						} */
						 if(data.message == '操作失败' || data.message == '没有默认办事处'){
								//不是服务经理
								isManage = 0;
							/*   $(".handle").remove();
								if(data.message == '操作失败') {
										
								} */
						} 
						
						if (data.hasOwnProperty("data")) {
								var manage = data.data;
								$('#addOrderForm').find('input[name="regionName"]').val(manage.region);
								$('#addOrderForm').find('input[name="handle"]').val(manage.handler);
								$('#addOrderForm').find('input[name="handleNo"]').val(manage.handlerNo);
								$('#addOrderForm').find('input[name="handlerDept"]').val(manage.region);
						}
				},
				error : function () {
				}
		});
	}
	//checkMannge();

	function initCheckBox(){
   		$('#addOrderForm').find('input[type=checkbox]').each(function(index,item){
   			var _title = $(item)[0].title;
   			var _checked = $(item)[0].checked
   			if(_title === '项目交付' || _title === '对外测试' || _title ==='备件维修'){
   				if(!_checked){
   					$(item).attr("disabled","disabled")
   					$(item).addClass('disabletextColor');
   				}
   			}
   		});
		$(".onlineProblemsName").val("");
		$('#addOrderForm').find('input[name=onlineOrderFromNo]').val("");
		addForm = form.render(null,'add-orderForm');
		
	}
	
	//initCheckBox();
    $('.layui-form-item.onlineProblems').hide();
    $('.layui-form-item.projectDelivery').hide();
	
	 form.on('checkbox()', function(data){
	   	var checked = $(this)[0].checked;
	   	var title  =$(this)[0].title;
	   	if(checked){
	   		if(title === '项目交付' || title === '对外测试' || title ==='备件维修'){
	   			$('.layui-form-item.projectDelivery').show();
		   		$('#addOrderForm').find('input[type=checkbox]').each(function(index,item){
		   			var _title = $(item)[0].title;
		   			var _checked = $(item)[0].checked
		   			if(_title === '项目交付' || _title === '对外测试' || _title ==='备件维修'){
		   				if(!_checked){
		   					$(item).attr("disabled","disabled")
		   					$(item).addClass('disabletextColor');
		   				}
		   			}
		   		});
		   		
		   		if(title === '项目交付'){
   					clickType = 1;
   					$('#type_title').html('<span class="redColor">*</span>项目名称:');
   				}
   				
   				if(title === '对外测试'){
   					clickType = 2;
   					$('#type_title').html('<span class="redColor">*</span>对外测试:');
   				}
   				
   				if(title === '备件维修'){
   					clickType = 3;
   					$('#type_title').html('<span class="redColor">*</span>备件维修:');
   				}
		   	}
	   		
	   		if(title === '网上问题'){
   				if(checked){
						$('.layui-form-item.onlineProblems').show();
   				}else{
   					$('.layui-form-item.onlineProblems').hide();
   					$(".onlineProblemsName").val("");
   				}
				}
	   	}else{
	   		if(title === '项目交付' || title === '对外测试' || title ==='备件维修'){
					clickType = 0;
	   			$('.layui-form-item.projectDelivery').hide();
	   			$(".projectName").val("");
	   		}
	   		if(title === '网上问题'){
   				if(checked){
						$('.layui-form-item.onlineProblems').show();
   				}else{
   					$('.layui-form-item.onlineProblems').hide();
						$(".onlineProblemsName").val("");
						$('#addOrderForm').find('input[name=onlineOrderFromNo]').val("");
   				}
			}
	   		$('#addOrderForm').find('input[type=checkbox]').each(function(index,item){
	   			if(title === '项目交付' || title === '对外测试' || title ==='备件维修'){
	   				$(item).removeAttr("disabled");
	   				$(item).removeClass('disabletextColor');
	   			}
	   		});
	   	}
	   	addForm = form.render(null,'add-orderForm');
	  });
	
	
	form.on('submit(add-order)',function(data){
		data.field.creator = window.empName;
		data.field.creatorDept = window.empDept;
		data.field.creatorNo = window.empCode;
		
		var type = [];
		$('#addOrderForm').find('input[type=checkbox]').each(function(index,item){
			var _title = $(item)[0].title;
   			var _checked = $(item)[0].checked;
   			if(_checked){
   				type.push(_title);
   			}
		});
		data.field.orderFrom = type.join(',');
		
		/*var isCheck = false;
		$('#addOrderForm').find('input[type=checkbox]').each(function(index,item){
   			isCheck = $(item)[0].checked;
   			console.log('记录值：'+isCheck);
   			return false;
   		});
        if(!isCheck){
            layer.msg('请选择一个工单类型');
            return false;
        }*/

		var arr = $('#addOrderForm').find('.layui-form-checked');
		if(arr.length <= 0) {
			layer.msg('请选择一个工单类型');
			return false;
		}

		var content = data.field;
		if(content.projectName == "" || content.onlineProblemsName == "") {
			if (content.orderFrom.indexOf("项目交付") > -1 &&　content.projectName == "") {
				layer.msg('请选择交付项目');
				return false;
			}
			if (content.orderFrom.indexOf("对外测试") > -1 && content.projectName == "") {
				layer.msg('请选择对外测试项目');
				return false;
			}
			if (content.orderFrom.indexOf("备件维修") > -1 && content.projectName == "") {
				layer.msg('请选择备件维修工单');
				return false;
			}
			if (content.orderFrom.indexOf("网上问题") > -1 && content.onlineProblemsName == "") {
				layer.msg('请选择网上问题');
				return false;
			}
		}
		content.orderFromName = content.projectName;
// 		if(content.onlineProblemsName != "") {
// 				content.orderFromName = content.onlineProblemsName;
// 		}
		var ccList = [""];
		if (selectCCList.length > 0) {
			for(var cci = 0; cci < selectCCList.length; cci++) {
				ccList.push(selectCCList[cci].empCode);
			}
		}
		content.ccList = ccList;
		
		var _index = layer.load(3);
		var id = $("#addOrderForm").find('input[name="id"]').val();
		console.log(id);
		
		$.ajax({
			url:window.basePath +'/order/addOrder',
			data:content,
			headers: {"token" : window.token},
			method: "post",
			success:function(data){
				layer.close(_index);
				if(data.success){
					$('#addOrderForm')[0].reset();
				}
				if( id != 0) {
					layer.msg("修改成功");
					setTimeout(function(){
						window.location.href = "index#/orders/orders";
					},1000);
				}else {
						layer.msg(data.message);
				}
			},
			error:function(data){
				layer.close(_index);
				layer.msg('网络异常');
			}
		});
		
		return false;
	});
	
	$('#onlineProblems').on('click',function(){
		showDialog({
			title:'网上问题',		
			showbtns:true,
			template:window.basePath + '/admin/views/orders/OnlineProblems.html',
			yes:function(index,layero){
				var checkStatusTemp = window.onlineProblemsTable.checkStatus('onlineProblemsTable');
				var data = checkStatusTemp.data;
				if(data && data.length >0){
					if(data.length>1){
						layer.msg('只能选择一条数据');
					}else{
						$('#addOrderForm').find('input[name=customerName]').val(data[0].company_name);
						$('#addOrderForm').find('input[name=orderFromName]').val(data[0].title);
						$('#addOrderForm').find('input[name=onlineProblemsName]').val(data[0].title);
						// $('#addOrderForm').find('input[name=orderFromNo]').val(data[0].request_number);
						$('#addOrderForm').find('input[name=onlineOrderFromNo]').val(data[0].request_number);
						$('#addOrderForm').find('input[name=customAddress]').val(data[0].company_address);
						$('#addOrderForm').find('input[name=customerPhone]').val(data[0].company_phone);
						layer.closeAll();
					}
				}
			}
		});
	});
	
	$('#projectDelivery').on('click',function(){
		if(clickType == 1){
			showDialog({
				title:'项目交付',	
				showbtns:true,
				template:window.basePath + '/admin/views/orders/ProjectDelivery.html',
				yes:function(index,layero){
					var checkStatusTemp = window.projectDeliveryTable.checkStatus('projectDeliveryTable');
				    var data = checkStatusTemp.data;
				    if(data && data.length >0){
				    	if(data.length>1){
							layer.msg('只能选择一条数据');
						}else{
								$('#addOrderForm').find('input[name=customerName]').val(data[0].originalCustomerName);
								$('#addOrderForm').find('input[name=contactName]').val(data[0].contactPerson);
								$('#addOrderForm').find('input[name=orderFromName]').val(data[0].projectName);
								$('#addOrderForm').find('input[name=projectName]').val(data[0].projectName);
								$('#addOrderForm').find('input[name=orderFromNo]').val(data[0].projectNo);
						    $('#addOrderForm').find('input[name=customerAddress]').val(data[0].contactAddress);
						    $('#addOrderForm').find('input[name=customerPhone]').val(data[0].contactTel);
						    layer.closeAll();
						}
				    }
				}
			});
		}
		
		if(clickType == 2){
			showDialog({
				title:'对外测试',		
				showbtns:true,
				template:window.basePath + '/admin/views/orders/ExternalTest.html',
				yes:function(index,layero){
					  var checkStatusTemp = window.externalTable.checkStatus('externalTestTable');
				      var data = checkStatusTemp.data;
				      if(data && data.length >0){
				    	  if(data.length>1){
								layer.msg('只能选择一条数据');
							}else{
								$('#addOrderForm').find('input[name=customerName]').val(data[0].kehumc);
									$('#addOrderForm').find('input[name=orderFromName]').val(data[0].xiangmumc);
							    $('#addOrderForm').find('input[name=externalTestName]').val(data[0].xiangmumc);
							    $('#addOrderForm').find('input[name=projectName]').val(data[0].xiangmumc);
							    $('#addOrderForm').find('input[name=orderFromNo]').val(data[0].WF_DocNumber);
							    layer.closeAll();
							}
				      }
				}
			});
		}
		
		if(clickType == 3){
			showDialog({
				title:'备件维修',		
				showbtns:true,
				template:window.basePath + '/admin/views/orders/Ram.html',
				yes:function(index,layero){
					var checkStatusTemp = window.rmaTable.checkStatus('rmaTable');
				    var data = checkStatusTemp.data;
				    if(data && data.length >0){
				    	if(data.length>1){
							layer.msg('只能选择一条数据');
						}else{
							 $('#addOrderForm').find('input[name=customerName]').val(data[0].customer_name);
						      $('#addOrderForm').find('input[name=orderFromName]').val(data[0].code_confirm);
									$('#addOrderForm').find('input[name=projectName]').val(data[0].code_confirm);
						      $('#addOrderForm').find('input[name=rmaName]').val(data[0].code_confirm);
						      $('#addOrderForm').find('input[name=orderFromNo]').val(data[0].work_order_number);
						      $('#addOrderForm').find('input[name=contactName]').val(data[0].addressee);
						      $('#addOrderForm').find('input[name=customerPhone]').val(data[0].addressee_phone);
						      layer.closeAll();
						}
				    }
				}
			});
		}
		
	});
	
	
	$('#selectHandler').on('click',function(){
		showDialog({
			title:'选择工程师',			
			template:window.basePath + '/admin/views/sys/usersSelect.html',
			showbtns:true,
			width:750,
			height:550,
			yes:function(index){
				var checkTempData = window.userTable.checkStatus('userTable');
				var data = checkTempData.data;
				if(data && data.length >0){
					if(data.length>1){
						layer.msg('只能选择一条数据');
					}else{
						$('#addOrderForm').find('input[name="handle"]').val(data[0].EmpName);
						$('#addOrderForm').find('input[name="handleNo"]').val(data[0].EmpCode);
						$('#addOrderForm').find('input[name="handlerDept"]').val(data[0].regionName);
						layer.close(index);
					}
				}
			}
		});
	});

    $('#selectRegion').on('click',function(){
        showDialog({
            title:'选择办事处\部门',
            template:window.basePath + '/admin/views/sys/regions.html',
            showbtns:true,
            width:750,
            height:550,
            yes:function(index){
                var checkTempData = window.regionTable.checkStatus('regionTable');
                var data = checkTempData.data;
                if(data && data.length >0){
                    if(data.length>1){
                        layer.msg('只能选择一条数据');
                    }else{
                        $('#addOrderForm').find('input[name="regionName"]').val(data[0].regionName);
                        $('#addOrderForm').find('input[name="handle"]').val(data[0].manager);
                        $('#addOrderForm').find('input[name="handleNo"]').val(data[0].workNo);
                        $('#addOrderForm').find('input[name="handlerDept"]').val(data[0].regionName);
                        layer.close(index);
                    }
                }
            }
        });
    });
		
		$('#selectCC').on('click',function(){
				showDialog({
						title:'选择抄送人',
						template:window.basePath + '/admin/views/sys/cc.html',
						showbtns:true,
						width:750,
						height:550,
						yes:function(index){
								var checkTempData = window.ccTable.checkStatus('ccTable');
								console.log(checkTempData);
								var data = checkTempData.data;
								var nameArray = [];
								if(data && data.length >0) {
									if (selectCCList.length > 0) {
										// 合并
										$.each(data, function(jndex, newItem) {
											var flag = true;
											var i = 0;
											$.each(selectCCList, function(index, oldItem) {
												if(newItem.EmpCode == oldItem.EmpCode) {
													flag = false;
													return;
												}
											});
											if(flag) {
												selectCCList.push(newItem);
											}
										});
									} else {
										selectCCList = data;
									}
								}
								for(var si = 0; si < selectCCList.length; si ++) {
									nameArray.push(selectCCList[si].EmpName);
								}
								$("#ccName").val(nameArray.join(","));
								layer.close(index);
						}
				});
		});
		
		$("#remove-cc").on('click', function(e) {
			selectCCList = [];
			$("#ccName").val('');
		});
		
		$("#selectAddress").on('click', function(e) {
			showDialog({
					title:'选择客户地址',
					template: window.basePath + '/admin/views/sys/address.html',
					showbtns: true,
					width: 750,
					height: 550,
					yes: function(index){
						console.log("选中的地址是：" + window._customerLocation);
						var ak = "83d2a8f1731ffd9706874206a8b5989f";
						var apiUrl = "https://restapi.amap.com/v3/geocode/regeo?output=json&location=" + window._customerLocation + "&key=" + ak + "&radius=5000&extensions=base&";
						var ajaxParams = {
							url: apiUrl,
							success: function(result) {
								console.log(result);
								if(result.status == "1") {
									window.customerAddress = {
										address: result.regeocode.formatted_address,
										location: window._customerLocation
									}
									$('#addOrderForm').find('input[name="customerAddress"]').val(result.regeocode.formatted_address);
									$('#addOrderForm').find('input[name="customerLocation"]').val(window._customerLocation);
									layer.close(index);
								} else {
									layer.msg('无法获取地理位置，请重试');
								}
							}, 
							error: function(err) {
								layer.msg('无法获取地理位置，请重试');
							}
						}
						$.ajax(ajaxParams);
					}
			});
		});
		
		// 初始化抄送人
		function initCCList() {
			$.ajax({
				url:window.basePath + '/order/cc/get?username=' + window.empCode,
				headers: {"token" : window.token},
				method: "post",
				success : function (result) {
					if(result.success) {
						selectCCList = result.data;
						var nameArray = [];
						for(var si = 0; si < selectCCList.length; si ++) {
							nameArray.push(selectCCList[si].empName);
						}
						$("#ccName").val(nameArray.join(","));
					}
				}
			});
		}
		initCCList();
		
    //获取参数
    function UrlSearch() {
        var obj = [];
        var value;
        var str=window.location.href; //取得整个地址栏
        var num=str.lastIndexOf("?");
        str=str.substr(num+1); //取得所有参数   stringvar.substr(start [, length ]
        var arr=str.split("&"); //各个参数放到数组里
        for(var i=0;i < arr.length;i++){
            num=arr[i].indexOf("=");
            if(num>0){
                value=arr[i].substr(num+1);
                obj.push(value);
            }
        }
				console.log(obj);
        if(obj.length > 0) {
            var id = parseInt(obj[0]);
            var type = obj[1];
						
						if (!id) {
							return;
						}
						
            $.ajax({
                url:window.basePath + '/order/orderDetail?orderId=' + id,
								headers: {"token" : window.token},
								method: "post",
                success : function (result) {
                    var data = result.data;
                    if(type == 'child') {
                        $('#addOrderForm').find('input[name="parentId"]').val(data.id);
                    }
                    if(type == 'update') {
                        $('#addOrderForm').find('input[name="id"]').val(data.id);
                        if(data.isUrgent === 0){
                        	$('#addOrderForm').find('input[name="isUrgent"][value=0]').attr("checked",true); 
                        }else{
                        	$('#addOrderForm').find('input[name="isUrgent"][value=1]').attr("checked",true); 
                        }
                        
                    }
                    $('#addOrderForm').find('input[name="customerName"]').val(data.customerName);

                    var orderFrom = data.orderFrom.split(',');
//                    console.log(orderFrom);
                    for(i=0;i<orderFrom.length;i++) {
                        if(orderFrom[i] == "项目交付" || orderFrom[i] == "对外测试" || orderFrom[i] == "备件维修"){
                            $('#addOrderForm').find('input[name="orderFrom"]').each(function () {
                                if($(this).attr("title") == orderFrom[i]) {
                                    $(this).next().addClass("layui-form-checked");
                                    $('.layui-form-item.projectDelivery').show();
                                    $(this).next().trigger("click");
                                    $('#addOrderForm').find('input[name="projectName"]').val(data.orderFromName);
                                    return false;
                                }
                            });
                        }else if(orderFrom[i] == "网上问题") {
													$('#addOrderForm').find('input[name="orderFrom"]').each(function () {
															if($(this).attr("title") == orderFrom[i]) {
																	$(this).next().addClass("layui-form-checked");
																	$('.layui-form-item.onlineProblems').show();
																	$(this).next().trigger("click");
																	$('#addOrderForm').find('input[name="onlineProblemsName"]').val(data.onlineProblemsName);
																	$('#addOrderForm').find('input[name=onlineOrderFromNo]').val(data.onlineOrderFromNo);
																	return false;
															}
													});
                        }else {
													$('#addOrderForm').find('input[name="orderFrom"]').each(function () {
															if($(this).attr("title") == orderFrom[i]) {
																	$(this).next().addClass("layui-form-checked");
																	$(this).next().trigger("click");
																	return false;
															}
													});
                        }
                    }
                    var isUrgent = data.isUrgent;
                    $("#addOrderForm").find('input[name="isUrgent"]').each(function () {
                        if($(this).val() == isUrgent) {
                        	 $(this).next().find('i').trigger("click");
                        }
                    })
                    if(data.hasOwnProperty("projectName")) {
											$('#addOrderForm').find('input[name="projectName"]').val(data.projectName);
                    }
										if (data.hasOwnProperty("orderFromNo")) {
											$('#addOrderForm').find('input[name="orderFromNo"]').val(data.orderFromNo);
										}
										if (data.hasOwnProperty("orderFromName")) {
											$('#addOrderForm').find('input[name="orderFromName"]').val(data.orderFromName);
										}
                    if(data.hasOwnProperty("onlineProblemsName")) {
                        $('#addOrderForm').find('input[name="onlineProblemsName"]').val(data.onlineProblemsName);
                    }
										if(data.hasOwnProperty("onlineOrderFromNo")) {
                        $('#addOrderForm').find('input[name="onlineOrderFromNo"]').val(data.onlineOrderFromNo);
                    }
                    if(data.hasOwnProperty("customerAddress")) {
                        $('#addOrderForm').find('input[name="customerAddress"]').val(data.customerAddress);
                    }
                    if(data.hasOwnProperty("contactName")) {
                        $('#addOrderForm').find('input[name="contactName"]').val(data.contactName);
                    }
                    if(data.hasOwnProperty("customerPhone")) {
                        $('#addOrderForm').find('input[name="customerPhone"]').val(data.customerPhone);
                    }
                    if(data.hasOwnProperty("taskDesc")) {
                        $('#addOrderForm').find('textarea[name="taskDesc"]').val(data.taskDesc);
                    }
                    if(data.hasOwnProperty("planTime")) {
                        $('#addOrderForm').find('input[name="planTime"]').val(data.planTime);
                    }
                    if(data.hasOwnProperty("orderNo")) {
                        $('#addOrderForm').find('input[name="orderNo"]').val(data.orderNo);
                    }
                   
                    if(data.hasOwnProperty("handle")) {
                         $('#addOrderForm').find('input[name="handle"]').val(data.handle);
                         $('#addOrderForm').find('input[name="handleNo"]').val(data.handleNo);
                         $('#addOrderForm').find('input[name="handlerDept"]').val(data.handlerDept);
                         $('#addOrderForm').find('input[name="regionName"]').val(data.handlerDept);
                    }
                    
                    if(type == 'child' || type == 'copy' || type == 'update') {
                    	 $('#addOrderForm').find('input[name="handle"]').val(data.initUserName);
                         $('#addOrderForm').find('input[name="handleNo"]').val(data.initWorkNo);
                         $('#addOrderForm').find('input[name="handlerDept"]').val(data.initDept);
                         $('#addOrderForm').find('input[name="regionName"]').val(data.initDept);
                    }
                }
            })
        }
    }
    UrlSearch();

</script>
</body>
</html>